/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InfosFrame.java
 *
 * Created on 20 déc. 2010, 22:57:24
 */

package org.epsi.blokus.server;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author julien.lafont
 */
public class InfosFrame extends javax.swing.JFrame implements Observer {

    // Formateur de date
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
    /** Creates new form InfosFrame */
    public InfosFrame() {
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNbJoueurs = new javax.swing.JTextField();
        txtNbSalons = new javax.swing.JTextField();
        btnQuitter = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtareaLog = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Serveur Blokus");
        setMinimumSize(new java.awt.Dimension(337, 445));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Nombre de joueurs connectés :");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Nombre de salons créés :");

        txtNbJoueurs.setEditable(false);
        txtNbJoueurs.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNbJoueurs.setText("0");

        txtNbSalons.setEditable(false);
        txtNbSalons.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNbSalons.setText("0");

        btnQuitter.setText("Quitter");
        btnQuitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitterActionPerformed(evt);
            }
        });

        txtareaLog.setColumns(20);
        txtareaLog.setEditable(false);
        txtareaLog.setRows(5);
        txtareaLog.setMargin(new java.awt.Insets(5, 5, 5, 5));
        jScrollPane1.setViewportView(txtareaLog);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNbSalons)
                            .addComponent(txtNbJoueurs, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                        .addComponent(btnQuitter)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtNbJoueurs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNbSalons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(btnQuitter)))
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnQuitterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitterActionPerformed
       this.dispose();
    }//GEN-LAST:event_btnQuitterActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnQuitter;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtNbJoueurs;
    private javax.swing.JTextField txtNbSalons;
    private javax.swing.JTextArea txtareaLog;
    // End of variables declaration//GEN-END:variables

    /**
     * Fonction appelée à la fermeture de la fenêtre
     */
    @Override
    public void dispose() {

        this.addLine("Server > Fermeture forcée des connexions");

        // TODO Fermer serveur correctement

        super.dispose();
        System.exit(1);
    }

    /**
     * Le serveur envoie un message au GUI : on le traite
     * @param o Source
     * @param arg Message
     */
    public void update(Observable o, Object m) {

        try {

            // On convertis le message reçu en un objet de la classe MessageInterne
            MessageInterne message = (MessageInterne)m;

            switch(message.getTypeMessage())
            {
                // Ajouter une ligne aux logs
                case ajouter_ligne:
                    this.addLine((String)message.getObject("LINE"));
                break;

                // Mettre à jour le nombre de salon
                case maj_nb_salons:
                    this.txtNbSalons.setText((String)message.getObject("NB"));
                break;

                // Mettre à jour le nombre de joueurs
                case maj_nb_joueurs:
                    this.txtNbJoueurs.setText((String)message.getObject("NB"));
                break;
            }
        
        } catch(Exception e ) {
          this.addLine("GUI > Réception d'un message incorrect");
        }
    }


    /**
     * Ajouter une ligne de log
     * @param str
     */
    public void addLine(String str)
    {
        Date now = new Date();
        this.txtareaLog.setText(this.txtareaLog.getText()+"["+dateFormat.format(now)+"]  "+str+"\n");
    }



}
